﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Resources;

namespace TMTemplate
{
	public partial class Sound : UserControl
	{
		private bool isMuted = false;
		public Sound()
		{
			InitializeComponent();
		}

        [Category("Common Properties")]
        public TimeSpan TimeShift
        {
            get;
            set;
        }

		[Category("Common Properties")]
        public string Content3
        {
            get { return txt2.Text; }
            set { txt2.Text = value; }	
        }

		[Category("Common Properties")]
        public string Content2
        {
            get { return txt.Text; }
            set { txt.Text = value; }	
        }

        [Category("Brushes")]
        public Brush Foreground1
        {
            get { return txt.Foreground; }
            set { txt.Foreground = value; }
        }

		[Category("Common Properties")]
        public string Content1
        {
            get { return RootTxt.Text;}
            set { RootTxt.Text = value; }
        }
		
        private void Loop_wma_MediaOpened(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DesignerProperties.IsInDesignTool)
            {
                (sender as MediaElement).IsMuted = true;
            }
        	TimelineMarker marker = new TimelineMarker();

            marker.Time = TimeShift;
            marker.Text = "loop";
            marker.Type = "loop";
            (sender as MediaElement).Markers.Add(marker);
            (sender as MediaElement).MarkerReached += Loop_wma_MarkerReached;
			Loop_wma.Play();
        }

        private void Loop_wma_MarkerReached(object sender, System.Windows.Media.TimelineMarkerRoutedEventArgs e)
        {
        	MediaElement loop = new MediaElement();
            loop.MediaOpened += Loop_wma_MediaOpened;
            loop.Name = "Loop_wma";
            loop.Volume = (sender as MediaElement).Volume;
			SoundLoop.Children.Clear();
            SoundLoop.Children.Add(loop);
            StreamResourceInfo sri = Application.GetResourceStream(new Uri("TMTemplate;component/Sound/Loop.wma", UriKind.RelativeOrAbsolute));
            loop.SetSource(sri.Stream);
            loop.Play();
        }

		private void LayoutRoot_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			if (!isMuted)
            {
                sbSoundFadeOut.Begin();
                VisualStateManager.GoToState(this, "MouseOver", true);
                isMuted = true;
            }
            else
            {
                sbSoundFadeIn.Begin();
                VisualStateManager.GoToState(this, "Normal", true);
                isMuted = false;
            }
		}
	}
}